﻿using MiniExcelLibs;
using MiniExcelLibs.Attributes;
using System.Text;
using System.Text.RegularExpressions;

namespace _出租车单选题数据提取
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string input = "A 、商业性机动车第三者责任险";

            MatchCollection matches3 = Regex.Matches(input, @"(\w)\s、(.+?)(?=\w\s、|$)");

           
            foreach (Match match in matches3)
            {
               string[] ss= match.Value.Split(" 、");
              
            }

           
            //string input = "1 、经过大庆市的国道有( C )。A 、201    203B 、201    305C 、203    3012 、出租汽车客运经营者应当为旅客投保(C)。A 、商业性机动车第三者责任险B 、机动车交通事故责任强制保险C 、承运人责任险";

            // 提取所有部分的数据
            //MatchCollection matches = Regex.Matches(input, @"(\d+)\s*、(.*?)(?=\d+\s*、|$)");

            //foreach (Match match in matches)
            //{
            //    int partNumber = int.Parse(match.Groups[1].Value);
            //    string partData = match.Groups[2].Value.Trim();
            //    Console.WriteLine($"第{partNumber}部分数据:");
            //    Console.WriteLine($"答案: {ExtractAnswer(partData)}");
            //    Console.WriteLine($"选项: {ExtractOptions(partData)}");

            //}
            //string ExtractAnswer(string partData)
            //{
            //    Regex answerRegex = new Regex(@"\((\s*\S+\s*)\)");
            //    Match answerMatch = answerRegex.Match(partData);
            //    return answerMatch.Success ? answerMatch.Groups[1].Value : "";
            //}

            //// 提取选项
            //string ExtractOptions(string partData)
            //{
            //    Regex optionsRegex = new Regex(@"。(.*?)$");
            //    Match optionsMatch = optionsRegex.Match(partData);
            //    return optionsMatch.Success ? optionsMatch.Groups[1].Value.Trim() : "";
            //}
            List<QuestionCar> questionCars = new List<QuestionCar>();
            using (Stream stream = File.OpenRead("C:\\Users\\humengjian\\Desktop\\大庆巡游出租车区域题库(2).xlsx"))
            {
                List<string> list = new List<string>();
                var questionInputs = stream.Query<QuestionImportDataV1>().ToList();
                if (questionInputs == null || questionInputs.Count == 0)
                {
                    return;
                }
                StringBuilder sb = new StringBuilder();
                foreach (var questionInput in questionInputs)
                {
                    sb.Append(questionInput.Content);
                }
                string allContent = sb.ToString();
                allContent = allContent.Replace("\n", "");
                MatchCollection matches = Regex.Matches(allContent, @"(\d+)\s*、(.*?)(?=\d+\s*、|$)");

                foreach (Match match in matches)
                {
                    int partNumber = int.Parse(match.Groups[1].Value);
                    string partData = match.Groups[2].Value.Trim();
                    Regex regex = new Regex("(.*?)。");
                    MatchCollection matches2 = regex.Matches(partData);
                    string realContent = string.Empty;
                    if (matches2.Count > 0)
                    {
                        foreach (Match match2 in matches2)
                        {
                            realContent += match2;
                        }
                    }
                    else
                    {
                        realContent = partData;
                    }

                    //Console.WriteLine($"第{partNumber}部分数据:");
                    //Console.WriteLine($"题目：{realContent}");
                    //Console.WriteLine($"答案: {ExtractAnswer(partData)}");
                    //Console.WriteLine($"选项: {ExtractOptions(partData)}");

                    QuestionCar questionCar = new QuestionCar();
                    questionCar.Num = partNumber;
                    questionCar.Content = realContent;
                    questionCar.Options = ExtractOptions(partData);
                    questionCar.Answer = ExtractAnswer(partData);
                    questionCars.Add(questionCar);

                }
                int i = 0;
                foreach (var question in questionCars)
                {


                    i++;
                    if (question.Num == null || string.IsNullOrEmpty(question.Content) || string.IsNullOrEmpty(question.Options) || string.IsNullOrEmpty(question.Answer))
                    {
                        Console.WriteLine($"上一行数据的题目：{questionCars[i - 2].Content}");
                        //Console.WriteLine($"第{i}行数据:");
                        Console.WriteLine($"第{question.Num}部分数据:");
                        Console.WriteLine($"题目：{question.Content}");
                        Console.WriteLine($"答案: {ExtractAnswer(question.Options)}");
                        Console.WriteLine($"选项: {ExtractOptions(question.Answer)}");
                    }

                }
                using (var strem = File.Create("output.xlsx"))
                {
                    strem.SaveAs(questionCars);
                }
                string ExtractAnswer(string partData)
                {
                    Regex answerRegex = new Regex(@"\((\s*\S+\s*)\)");
                    Match answerMatch = answerRegex.Match(partData);
                    return answerMatch.Success ? answerMatch.Groups[1].Value : "";
                }
                // 提取选项
                string ExtractOptions(string partData)
                {
                    Regex optionsRegex = new Regex(@"。(.*?)$");
                    Match optionsMatch = optionsRegex.Match(partData);
                    return optionsMatch.Success ? optionsMatch.Groups[1].Value.Trim() : "";
                }

            }
        }
    }
    public class QuestionImportDataV1
    {
        [ExcelColumnName("单项选择题")]
        public string Content { get; set; }


    }

    public class QuestionCar
    {
        [ExcelColumnName("序号")]
        public int? Num { get; set; }
        [ExcelColumnName("内容")]
        public string Content { get; set; }
        [ExcelColumnName("选项")]
        public string Options { get; set; }
        [ExcelColumnName("答案")]
        public string Answer { get; set; }
    }
}
